const express = require('express');
const router = express.Router();
const db = require('../../Models')
const debug = require('debug')('server:member')

/*
*  角色管理
*/
router.get('/role',(req,res)=>{
    res.render('member/role');
})

router.get('/roleAdd',(req,res)=>{
    res.render('member/roleAdd');
})

router.post('/roleAddTo', async (req,res)=>{
    
    let result = await db.role.findOrCreate({
        where: {
            name: req.body.name
        }
    });
    console.log();
    res.json(result[1])
    //res.json({title: '11111'})
})

router.get('/roleEdit',(req,res) =>{
    let roleName = req.query.name || '';
    res.render('member/roleEdit',{
        name: roleName,
    });
})

router.get('/roleDel',async (req,res)=>{
    let id = (req.query.id).split(',') || 0;
    let result = await db.role.destroy({
        where: {
            id: {
                $in: [id]
            }
        }
    })
    res.json(result);
})

router.post('/roleEditTo', async (req,res)=>{
    let id = req.body.id || 0;
    let roleName = req.body.name || '';

    let result = await db.role.update({
        name: roleName
    }, {
            where: {
                id: id
            }
        });
    res.json(result);
})

router.get('/roleList', async (req,res)=>{
    
    let result = await db.role.findAll();
    res.json(result);
})

/*
 *用户列表 
 */

router.get('/user',(req,res)=>{
    res.render('member/user');
})

router.get('/userAdd',  async (req,res)=>{
    let result = await db.role.findAll();
    res.render('member/userAdd',{result});
})

router.get('/userEdit',async (req,res) =>{
    let userName = req.query.name || '';
    let roleId  =  req.query.roleId || 1;

    let result = await db.role.findAll();
    res.render('member/userAdd',{result});

    res.render('member/userEdit',{
        name: userName,
        roleId: roleId,
        result: result
    });
})

router.post('/userEditTo', async (req,res)=>{
    let id = req.body.id || 0;
    let userName = req.body.name || '';

    let result = await db.user.update({
        name: roleName
    }, {
            where: {
                id: id
            }
        });
    res.json(result);
})

router.get('/userList',async (req,res)=>{
    
    let name = req.query.name || ''
    let dateStart = req.query.dateStart || '0'
    let dateEnd = req.query.dateEnd || new Date
    let result = await db.user.findAll({
        include: [
            {model: db.role, required: true}
        ],    
        where: {
            name: {
                $like: `%${name}%`
            },
            createdAt: {
                $between: [dateStart, dateEnd]
               // $between: ['2018-10-1', '2018-12-11']
            }
        }
    });

    res.json(result);
})

router.get('/userDel',async (req,res)=>{
    let id = (req.query.id).split(',') || 0;
    let result = await db.user.destroy({
        where: {
            id: {
                $in: [id]
            }
        }
    })
    res.json(result);
})


module.exports = router;